/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.datasource;


import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.request.ImageFrom;
import me.panpf.sketch.util.Log;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.utils.net.Uri;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import me.panpf.sketch.Gif;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * 用于读取来自本地的图片
 */
public class FileDataSource implements DataSource {

    @NotNull
    private Uri uri;
    private long length = -1;
    private Context context;

    public FileDataSource(Context context, @NotNull Uri uri) {
        this.uri = uri;
        this.context = context;
    }

    @NotNull
    @Override
    public InputStream getInputStream() throws IOException {

        try {
            return DataAbilityHelper.creator(context).obtainInputStream(uri);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public synchronized long getLength() throws IOException {

        return length;
    }

    @Override
    public File getFile(@Nullable File outDir, @Nullable String outName) {
        try {
            if (outDir != null) {
                File file = new File(outDir.getPath() + "/" + outName);
                copyInputStreamToFile(getInputStream(), file);
                return file;
            }
        } catch (Exception ignored) {
            Log.error("sketch", ignored.getMessage());
        }
        return new File("");
    }

    private static void copyInputStreamToFile(InputStream inputStream, File file)
            throws IOException {
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            int read;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }
    }

    @NotNull
    @Override
    public ImageFrom getImageFrom() {
        return ImageFrom.LOCAL;
    }

    @NotNull
    @Override
    public Gif makeGifDrawable(@NotNull String key, @NotNull String uri, @NotNull ImageAttrs imageAttrs,
                               @NotNull BitmapPool bitmapPool) throws Exception {
//        return SketchGifFactory.createGifDrawable(key, uri, imageAttrs, getImageFrom(), bitmapPool, getInputStream());
//        return GifFactory.createGif(key, uri, imageAttrs, getImageFrom(), bitmapPool, getInputStream());
//        return GifFactory.createGif(key, uri, imageAttrs, getImageFrom(), bitmapPool, context, 0);
        return new Gif(context, getInputStream());
    }
}
